import java.util.Scanner;

public class NodeA {

    // 定义 Friend 类，用于存储联系人信息
    public class Friend {
        public Friend next;    // 指向下一个节点
        public Friend prev;    // 指向前一个节点（新增，用于双链表）
        private String Name;
        private String Tel;
        private String Email;
        private String BornAdd;

        // 构造函数
        public Friend(){}

        // 带参数的构造函数
        public Friend(String Name, String Tel, String Email, String BornAdd) {
            this.Name = Name;
            this.Tel = Tel;
            this.Email = Email;
            this.BornAdd = BornAdd;
        }

        // Getter 和 Setter 方法
        public String getName() {
            return Name;
        }

        public void setName(String Name) {
            this.Name = Name;
        }

        public void setTel(String Tel) {
            this.Tel = Tel;
        }

        public String getTel() {
            return Tel;
        }

        public void setEmail(String Email) {
            this.Email = Email;
        }

        public String getEmail() {
            return Email;
        }

        public void setBornAdd(String BornAdd) {
            this.BornAdd = BornAdd;
        }

        public String getBornAdd() {
            return BornAdd;
        }

        // 更新联系人信息
        public void message(String Name, String Tel, String Email, String BornAdd) {
            this.Name = Name;
            this.Tel = Tel;
            this.Email = Email;
            this.BornAdd = BornAdd;
        }

        // 返回联系人信息的字符串表示
        public String toString() {
            return this.Name + "\t" + this.Tel + "\t" + this.Email + "\t" + this.BornAdd;
        }
    }

    private int size;
    private Friend head;   // 链表头节点

    // 初始化链表
    {
        this.size = 0;
        this.head = null;
    }

    // 在链表头部插入新节点（改为双链表逻辑）
    public void insertHead(Friend s) {
        if (this.size == 0) {
            this.head = s;
        } else {
            s.next = this.head;
            this.head.prev = s;  // 设置前驱节点（双链表逻辑）
            this.head = s;
        }
        this.size++;
    }

    // 删除指定姓名的联系人（补充完整）
    public boolean delete(String Name) {
        Friend front = this.head;

        // 如果头节点是要删除的节点
        if (front != null && Name.equals(front.getName())) {
            this.head = front.next;
            if (this.head != null) {
                this.head.prev = null;  // 更新头节点的前驱
            }
            this.size--;
            return true;
        }

        // 遍历链表查找要删除的节点
        while (front != null) {
            if (Name.equals(front.getName())) {
                // 调整前后节点的指针
                front.prev.next = front.next;
                if (front.next != null) {
                    front.next.prev = front.prev;
                }
                this.size--;
                return true;
            }
            front = front.next;
        }

        return false;  // 未找到要删除的节点
    }

    // 查询指定姓名的联系人
    public Friend select(String Name) {
        Friend front = this.head;

        if (Name.equals(front.getName())) {
            Friend s = new Friend(Name, front.getTel(), front.getEmail(), front.getBornAdd());
            return s;
        }

        while (front.next != null) {
            front = front.next;
            if (Name.equals(front.getName())) {
                Friend s = new Friend(Name, front.getTel(), front.getEmail(), front.getBornAdd());
                return s;
            }
        }

        return null;
    }

    // 显示链表中的所有联系人
    public void display() {
        System.out.println("姓名\t 手机号\t 邮箱地址\t 家庭住址");

        Friend front = this.head;
        while (front != null && front.getName() != null) {
            System.out.println(front.toString());
            front = front.next;
        }

        if (this.head == null || this.head.getName() == null) {
            System.out.println("无相关信息");
        }
    }

    public static void main(String[] args) {
        NodeA sm = new NodeA();
        Scanner in = new Scanner(System.in);
        boolean open = true;

        while (open) {
            printMenu();
            System.out.println("请输入菜单序号进行操作");
            int menu = -1;

            try {
                menu = in.nextInt();
            } catch (Exception e) {
                in = new Scanner(System.in);
            }

            switch (menu) {
                case 0: {
                    System.out.println("创建成功");
                    break;
                }
                case 1: {
                    System.out.println("请输入姓名");
                    String Name1 = in.next();

                    System.out.println("手机号，邮箱地址，宿舍地址（空格结束，回车确认插入）");
                    try {
                        String Tel = in.next();
                        String Email = in.next();
                        String BornAdd = in.next();
                        Friend news = sm.new Friend(Name1, Tel, Email, BornAdd);
                        sm.insertHead(news);
                    } catch (Exception e) {
                        System.out.println("输入信息有误");
                        in = new Scanner(System.in);
                    }
                    break;
                }
                case 2: {
                    System.out.println("请输入要查询联系人的姓名");
                    String Name2 = in.next();
                    Friend selectS = sm.select(Name2);
                    System.out.println("姓名\t 手机号\t 邮箱地址\t 宿舍地址");
                    if (selectS == null) {
                        System.out.println("未找到联系人");
                    } else {
                        System.out.println(selectS.toString());
                    }
                    break;
                }
                case 3: {
                    System.out.println("请输入要删除联系人的姓名");
                    String Name3 = in.next();
                    if (sm.delete(Name3)) {
                        System.out.println("删除成功");
                    } else {
                        System.out.println("删除失败");
                    }
                    sm.display();
                    break;
                }
                case 4: {
                    sm.display();
                    break;
                }
                case 5: {
                    System.out.println("系统已退出");
                    System.exit(0);
                    break;
                }
                default:
                    System.out.println("输入错误");
            }
        }

        in.close();
    }

    // 打印菜单
    public static void printMenu() {
        System.out.println("24软件1/2班宿舍管理系统");
        System.out.println("*****************");
        System.out.println("* 0----创建链表   *");
        System.out.println("* 1----插入联系人 *");
        System.out.println("* 2----查找联系人 *");
        System.out.println("* 3----删除联系人 *");
        System.out.println("* 4----输出通讯录 *");
        System.out.println("* 5----退出系统   *");
        System.out.println("*****************");
    }
}
//